import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { getDictOptions } from '#/utils/dict';
import { renderDict } from '#/utils/render';

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'jobName',
    label: '任务名称',
  },
  {
    component: 'Select',
    fieldName: 'jobGroup',
    label: '任务组名',
    componentProps: {
      // 可选从DictEnum中获取 DictEnum.SYS_NORMAL_DISABLE 便于维护
      options: getDictOptions('sys_job_group'),
      buttonStyle: 'solid',
      optionType: 'button',
    },
  },
  {
    component: 'Select',
    componentProps: {
      // 可选从DictEnum中获取 DictEnum.SYS_NORMAL_DISABLE 便于维护
      options: getDictOptions('sys_normal_disable'),
      buttonStyle: 'solid',
      optionType: 'button',
    },
    fieldName: 'status',
    label: '状态',
  },
];

// 需要使用i18n注意这里要改成getter形式 否则切换语言不会刷新
// export const columns: () => VxeGridProps['columns'] = () => [
export const columns: VxeGridProps['columns'] = [
  { type: 'checkbox', width: 60 },
  {
    title: '任务名称',
    field: 'jobName',
  },
  {
    title: '任务分组',
    field: 'jobGroup',
    slots: {
      default: ({ row }) => {
        // 可选从DictEnum中获取 DictEnum.SYS_MISFIRE_POLICY 便于维护
        return renderDict(row.jobGroup, 'sys_job_group');
      },
    },
  },
  {
    title: '调用方法',
    field: 'invokeTarget',
  },
  {
    title: 'cron表达式',
    field: 'cronExpression',
  },
  {
    title: '执行策略',
    field: 'misfirePolicy',
    slots: {
      default: ({ row }) => {
        // 可选从DictEnum中获取 DictEnum.SYS_MISFIRE_POLICY 便于维护
        return renderDict(row.misfirePolicy, 'sys_misfire_policy');
      },
    },
  },
  {
    title: '是否并发',
    field: 'concurrent',
    slots: {
      default: ({ row }) => {
        // 可选从DictEnum中获取 DictEnum.SYS_IS_CONCURRENT 便于维护
        return renderDict(row.concurrent, 'sys_is_concurrent');
      },
    },
  },
  {
    title: '状态',
    field: 'status',
    slots: { default: 'status' },
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 200,
  },
];

export const modalSchema: FormSchemaGetter = () => [
  {
    label: '任务ID',
    fieldName: 'jobId',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '任务名称',
    fieldName: 'jobName',
    component: 'Input',
    rules: 'required',
  },
  {
    component: 'Select',
    fieldName: 'jobGroup',
    label: '任务组名',
    rules: 'required',
    componentProps: {
      // 可选从DictEnum中获取 DictEnum.SYS_NORMAL_DISABLE 便于维护
      options: getDictOptions('sys_job_group'),
      buttonStyle: 'solid',
      optionType: 'button',
    },
  },
  {
    label: '调用方法',
    fieldName: 'invokeTarget',
    component: 'Input',
    help: '支持i18n写法, 如: menu.system.user',
    rules: 'required',
  },
  {
    label: 'cron表达式',
    fieldName: 'cronExpression',
    component: 'Input',
    rules: 'required',
  },
  {
    label: '执行策略',
    fieldName: 'misfirePolicy',
    component: 'RadioGroup',
    componentProps: {
      // 可选从DictEnum中获取 DictEnum.SYS_MISFIRE_POLICY 便于维护
      options: getDictOptions('sys_misfire_policy'),
      buttonStyle: 'solid',
      optionType: 'button',
    },
    defaultValue: '1',
    rules: 'selectRequired',
  },
  {
    label: '是否并发',
    fieldName: 'concurrent',
    component: 'RadioGroup',
    componentProps: {
      // 可选从DictEnum中获取 DictEnum.SYS_IS_CONCURRENT 便于维护
      options: getDictOptions('sys_is_concurrent'),
      buttonStyle: 'solid',
      optionType: 'button',
    },
    defaultValue: '1',
    rules: 'selectRequired',
  },
  {
    label: '状态',
    fieldName: 'status',
    component: 'RadioGroup',
    defaultValue: '1',
    componentProps: {
      // 可选从DictEnum中获取 DictEnum.SYS_NORMAL_DISABLE 便于维护
      options: getDictOptions('sys_normal_disable'),
      buttonStyle: 'solid',
      optionType: 'button',
    },
    rules: 'selectRequired',
  },
];
